#!/usr/bin/env python
# encoding: utf-8
'''
@file: xml_parser.py
@author: wangjianong
@time: 2020/5/6 16:15
@desc:解析voc的xml文件
'''

import xml.etree.ElementTree as ET
import os
import argparse

class XML_Parser:
    def __init__(self,xml_path):
        assert os.path.exists(xml_path)
        self.xml = ET.parse(open(xml_path,'r'))

    def get_node(self):
        root = self.xml.getroot()
        filename = root.find('filename').text
        print(filename)
        for obj in root.findall('object'):
            name = obj.find('name').text
            bndbox = obj.find('bndbox')
            # 可以通过类似路径的方式检索信息
            # bndbox = obj.find('bndbox/xmin')
            xmin = float(bndbox.find('xmin').text)
            ymin = float(bndbox.find('ymin').text)
            xmax = float(bndbox.find('xmax').text)
            ymax = float(bndbox.find('ymax').text)
            print(name,xmin,ymin,xmax,ymax)

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('xml_path',default=None,type=str)
    args = parser.parse_args()
    assert args.xml_path is not None
    return args


if __name__ == '__main__':
    args = get_args()
    xml_parser = XML_Parser(args.xml_path)
    xml_parser.get_node()




